package features.advance.leetcode.thread;

import java.util.ArrayList;
import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 *
 * 创建三个线程 一个打印a，一个打印n，一个打印t  循环交替打印antantant (编程语言不限)
 * @author LIN
 * @date 2021-04-30
 */
public class Solution11152 {

    private Integer variable = calculate();

    int calculate(){
        return 0;
    }
    public static void main(String[] args) {
        atomicImpl();
//        semaphoreImpl();
//        conditionImpl();
    }

    private static volatile AtomicInteger a = new AtomicInteger(1);
    private static AtomicInteger f = new AtomicInteger(0);
    private static int n = 3;
    public static void atomicImpl(){
        new Thread(() -> {
            for(;f.get() < n;){
                if (a.get()==2){
                    System.out.print("n");
                    a.compareAndSet(2,3);
                }
            }
        }, "t2").start();
        new Thread(() -> {
            for(;f.get() < n-1;){
                if (a.get()==1){
                    System.out.print("a");
                    a.compareAndSet(1,2);
                }
            }
        }, "t1").start();
        new Thread(() -> {
            for(;f.get() < n;){
                if (a.get()==3){
                    System.out.print("t");
                    a.compareAndSet(3,1);
                    f.getAndIncrement();
                }
            }
            System.out.println();
        }, "t3").start();
    }
        
    public static void semaphoreImpl(){
        Semaphore s1= new Semaphore(1);
        Semaphore s2= new Semaphore(0);
        Semaphore s3= new Semaphore(0);
        new Thread(() -> {
            for(;;){
                try {
                    s2.acquire(1);
                    System.out.println("n");
                    s3.release(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "t2").start();
        new Thread(() -> {
            for(;;){
                try {
                    s1.acquire(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                System.out.println("a");
                s2.release(1);
            }
        }, "t1").start();
        new Thread(() -> {
            for(;;){
                try {
                    s3.acquire(1);
                    System.out.println("t");
                    s1.release(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "t3").start();
    }
    public static void conditionImpl(){
        ReentrantLock lock = new ReentrantLock();
        Condition c1 = lock.newCondition();
        Condition c2 = lock.newCondition();
        Condition c3 = lock.newCondition();
        ArrayList<String> list = new ArrayList<>();
        list.add("t1");
        new Thread(() -> {
            for(;;){
                try {
                    lock.lock();
                    if (list.contains("t2")){
                        list.remove("t2");
                        System.out.println("n");
                        list.add("t3");
                        c3.signal();
                    }
                    c2.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "t2").start();
        new Thread(() -> {
            for(;;){
                try {
                    lock.lock();
                    if (list.contains("t3")){
                        list.remove("t3");
                        System.out.println("t");
                        list.add("t1");
                        c1.signal();
                    }
                    c3.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "t3").start();
        new Thread(() -> {
            for(;;){
                try {
                    lock.lock();
                    if (list.contains("t1")){
                        list.remove("t1");
                        System.out.println("a");
                        list.add("t2");
                        c2.signal();
                    }
                    c1.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "t1").start();
    }
}
